package com.everflourish.yeah100.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.everflourish.yeah100.R;
import com.everflourish.yeah100.act.markingsystem.TeacherShareActivity;
import com.everflourish.yeah100.entity.marking.TeacherModel;
import com.everflourish.yeah100.http.MarkingSystemRequest;
import com.everflourish.yeah100.ui.dialog.LoadDialog;
import com.everflourish.yeah100.utils.LogUtil;
import com.everflourish.yeah100.utils.MyToast;
import com.everflourish.yeah100.utils.constant.Constant;
import com.everflourish.yeah100.utils.constant.ResultCode;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：Created by liqisi on 2016/7/1.
 * 时间：2016/7/1
 */
public class TeacherShareAdapter extends RecyclerView.Adapter<TeacherShareAdapter.TeacherViewHolder> {

    private Context mContext;
    private TeacherShareActivity mActivity;
    private LayoutInflater mLayoutInflater;
    private List<TeacherModel> mList;
    private LoadDialog mLoadDialog;
    private RequestQueue mQueue;
    private MarkingSystemRequest mRequest;

    public TeacherShareAdapter(Context mContext, TeacherShareActivity mActivity,
                               List<TeacherModel> mList, RequestQueue mQueue) {
        this.mContext = mContext;
        this.mActivity = mActivity;
        this.mList = mList;
        this.mQueue = mQueue;
        mRequest = MarkingSystemRequest.getInstance();
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public TeacherViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mLayoutInflater.inflate(R.layout.item_teacher_share, parent, false);
        TeacherViewHolder viewHolder = new TeacherViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(TeacherViewHolder holder, int position) {
        TeacherModel teacherModel = mList.get(position);
        holder.checkBox.setChecked(teacherModel.isCheck());
        holder.userNameTv.setText(teacherModel.getTeacher().getUserName());
        if (!mActivity.isSelfCreate && !teacherModel.isDelete()) {
            holder.deleteLl.setVisibility(View.GONE);
        }
        if (!mActivity.isSelfCreate && teacherModel.isCheck()) {
            holder.checkBox.setClickable(false);
        } else {
            holder.checkBox.setOnClickListener(new MyOnclickListener(holder, position, teacherModel));
        }
        holder.deleteLl.setOnClickListener(new MyOnclickListener(holder, position, teacherModel));
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class TeacherViewHolder extends RecyclerView.ViewHolder {

        private CheckBox checkBox;
        private TextView userNameTv;
        private LinearLayout deleteLl;

        public TeacherViewHolder(View itemView) {
            super(itemView);
            checkBox = (CheckBox) itemView.findViewById(R.id.teacher_check_rb);
            userNameTv = (TextView) itemView.findViewById(R.id.teacher_name_tv);
            deleteLl = (LinearLayout) itemView.findViewById(R.id.teacher_delete_ll);
        }
    }

    class MyOnclickListener implements View.OnClickListener {

        private TeacherViewHolder viewHolder;
        private int position;
        private TeacherModel teacherModel;

        public MyOnclickListener(TeacherViewHolder viewHolder, int position, TeacherModel teacherModel) {
            this.viewHolder = viewHolder;
            this.position = position;
            this.teacherModel = teacherModel;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.teacher_select_ll:
                    break;
                case R.id.teacher_check_rb:
                    teacherModel.setIsCheck(viewHolder.checkBox.isChecked());
                    break;
                case R.id.teacher_delete_ll:
                    deleteShareTeacherRequest(teacherModel);
                    break;
                default:
                    break;
            }
        }
    }

    /**
     * 删除共享老师请求
     *
     * @param model
     */
    private void deleteShareTeacherRequest(final TeacherModel model) {
        mLoadDialog = LoadDialog.show(mContext, null, "移除中...", mQueue);
        List<String> ids = new ArrayList<String>();
        ids.add(model.getTeacher().getUserId());
        JsonObjectRequest request = mRequest.deteleShareTeacherRequest(ids,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        deleteShareTeacherListener(response, model);
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        mRequest.disposeError(mContext, mLoadDialog, error, "删除老师失败");
                    }
                });
        mQueue.add(request);
//        mQueue.start();
    }

    /**
     * 删除共享老师监听
     *
     * @param response
     * @param model
     */
    private void deleteShareTeacherListener(JSONObject response, TeacherModel model) {
        String resultCode = "";
        try {
            resultCode = response.getString(Constant.RESULT_CODE);
            if (resultCode.equals(ResultCode.result_ok.resultCode)) {
                mList.remove(model);
                this.notifyDataSetChanged();
            } else if (resultCode.equals(ResultCode.result_000001E.resultCode)) {
                MyToast.showShort(mContext,
                        R.string.mistakenotebook_10141_000001E);
            } else if (resultCode
                    .equals(ResultCode.result_system_exception_999999E)) {
                MyToast.showShort(mContext,
                        R.string.mistakenotebook_101399_999999E);
            } else {
                MyToast.showShort(mContext, "删除老师失败");
            }
        } catch (JSONException e) {
            LogUtil.e("JSON异常", e);
            MyToast.showShort(mContext, "删除老师失败");
        } finally {
            LoadDialog.dismiss(mLoadDialog);
        }
    }

}